package com.wlcj.www.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.ListUtils;
import org.nutz.ioc.loader.annotation.IocBean;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.jfinal.plugin.activerecord.Page;
import com.wlcj.constant.Global;
import com.wlcj.www.model.platform.PlatformNew;


/**
 * 平台service
 * @author huan
 *
 */
@IocBean
public class PlatformService {
	

	/**
	 * 平台列表加载  （传参）
	 * @param entity
	 * @return
	 */
	public Page<PlatformNew> getPage(Map<String , String [] > entity ){
		int pageNo = entity.get("pageNo")==null?1:Integer.parseInt(entity.get("pageNo")[0].toString());
		StringBuffer sql = new StringBuffer("from wd_p_platform_info where 1=1");
		Map<String , Object> result = new HashMap<String, Object>();
		if(entity!=null){
			result = getParam(entity, sql);
		}
		sql = new StringBuffer(entity.get("sql")==null?sql:entity.get("sql").toString());
		System.err.println(sql.toString()+"======================");
		System.err.println(result.get("sql")+"======================");
		if(CollectionUtils.isEmpty((Collection<List>) result.get("list"))){
			return PlatformNew.dao.paginate(pageNo, Global.PAGESIZE, "select *", sql.toString());
		}else{
			return PlatformNew.dao.paginate(pageNo, Global.PAGESIZE, "select *", sql.toString(), ((List)result.get("list")).toArray());
		}
		
	}
	
	public Map<String , Object> getParam (Map<String , String[] > entity , StringBuffer sql){
		/*comprehensive_rating
		average_interest_rate
		average_investment_horizon
		online_time
		province_id
		registered_capital
		security_model
		escrow
		creditor_rights_transfer
		platform_background
		platform_features*/
		Map<String , Object> result = new HashMap<String, Object>();
		List<String> list = new ArrayList<String>();
		if(entity.get("comprehensive_rating")!=null){//安全评级
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("comprehensive_rating").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" comprehensive_rating  = ? ");
				if(i==entity.get("comprehensive_rating").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("comprehensive_rating")[i]);
			}
		}
		
		if(entity.get("average_interest_rate")!=null){//年化收益
			sql.append(" and ( ");
			for( int i = 0 ; i < entity.get("average_interest_rate").length ; i ++){
				String [] average_interest_rate = entity.get("average_interest_rate")[i].split("-");
				if(i!=0){
					sql.append(" or ");
				}
				if(average_interest_rate.length==1){
					sql.append(" average_interest_rate  >= ?  ");
					list.add(average_interest_rate[0]);
				}
				if(average_interest_rate.length==2){
					sql.append(" average_interest_rate  >= ?  ");
					list.add(average_interest_rate[0]);
					sql.append(" and ");
					sql.append(" average_interest_rate  < ? ");
					list.add(average_interest_rate[1]);
				}
				if(i==entity.get("average_interest_rate").length-1){
					sql.append(" ) ");
				}
				
			}
		}
		
		if(entity.get("average_investment_horizon")!=null){//投资期限
			sql.append(" and ( ");
			for( int i = 0 ; i < entity.get("average_investment_horizon").length ; i ++){
				String [] average_interest_rate = entity.get("average_investment_horizon")[i].split("-");
				if(i!=0){
					sql.append(" or ");
				}
				if(average_interest_rate.length==1){
					sql.append(" average_investment_horizon  >= ?  ");
					list.add(average_interest_rate[0]);
				}
				if(average_interest_rate.length==2){
					sql.append(" average_investment_horizon  >= ?  ");
					list.add(average_interest_rate[0]);
					sql.append(" and ");
					sql.append(" average_investment_horizon  < ? ");
					list.add(average_interest_rate[1]);
				}
				if(i==entity.get("average_investment_horizon").length-1){
					sql.append(" ) ");
				}
				
			}
		}
		
		if(entity.get("online_time")!=null){//上线时间
			sql.append(" and ( ");
			for( int i = 0 ; i < entity.get("online_time").length ; i ++){
				String [] average_interest_rate = entity.get("online_time")[i].split("-");
				if(i!=0){
					sql.append(" or ");
				}
				if(average_interest_rate.length==1){
					sql.append(" online_time  >= ?  ");
					list.add(average_interest_rate[0]);
				}
				if(average_interest_rate.length==2){
					sql.append(" online_time  >= ?  ");
					list.add(average_interest_rate[0]);
					sql.append(" and ");
					sql.append(" online_time  < ? ");
					list.add(average_interest_rate[1]);
				}
				if(i==entity.get("online_time").length-1){
					sql.append(" ) ");
				}
				
			}
		}
		
		if(entity.get("province_id")!=null){//所在地区
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("province_id").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" province_id  = ? ");
				if(i==entity.get("province_id").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("province_id")[i]);
			}
		}
		
		if(entity.get("registered_capital")!=null){//注册资金
			sql.append(" and ( ");
			for( int i = 0 ; i < entity.get("registered_capital").length ; i ++){
				String [] average_interest_rate = entity.get("registered_capital")[i].split("-");
				if(i!=0){
					sql.append(" or ");
				}
				if(average_interest_rate.length==1){
					sql.append(" registered_capital  >= ?  ");
					list.add(average_interest_rate[0]);
				}
				if(average_interest_rate.length==2){
					sql.append(" registered_capital  >= ?  ");
					list.add(average_interest_rate[0]);
					sql.append(" and ");
					sql.append(" registered_capital  < ? ");
					list.add(average_interest_rate[1]);
				}
				if(i==entity.get("registered_capital").length-1){
					sql.append(" ) ");
				}
				
			}
		}
		
		if(entity.get("security_model")!=null){//保障模式
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("security_model").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" security_model  like ? ");
				if(i==entity.get("security_model").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("security_model")[i]);
			}
		}
		
		if(entity.get("escrow")!=null){//资金托管
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("escrow").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" escrow  = ? ");
				if(i==entity.get("escrow").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("escrow")[i]);
			}
		}
		
		if(entity.get("creditor_rights_transfer")!=null){//债权转让
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("creditor_rights_transfer").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" creditor_rights_transfer  = ? ");
				if(i==entity.get("creditor_rights_transfer").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("creditor_rights_transfer")[i]);
			}
		}
		
		if(entity.get("platform_background")!=null){//平台背景
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("platform_background").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" platform_background  = ? ");
				if(i==entity.get("platform_background").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("platform_background")[i]);
			}
		}
		
		if(entity.get("platform_features")!=null){//平台特征
			sql.append(" and (");
			for( int i = 0 ; i < entity.get("platform_features").length ; i ++){
				if(i!=0){
					sql.append(" or ");
				}
				sql.append(" platform_features  like ? ");
				if(i==entity.get("platform_features").length-1){
					sql.append(" ) ");
				}
				list.add(entity.get("platform_features")[i]);
			}
		}
		result.put("sql", sql);
		result.put("list", list);
		return result;
	}
}
